package org.example.hospital_registration_system.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.hospital_registration_system.model.LoginRespond;
import org.example.hospital_registration_system.model.User;
import org.example.hospital_registration_system.service.UserService;
import org.example.hospital_registration_system.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public LoginRespond login(User user){
        LoginRespond loginRespond = new LoginRespond();
        log.info("接收到的数据：{}",user);
        if(user==null|| !StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPassword())){
            loginRespond.setMsg("请合法输入");
            loginRespond.setUserId(null);
            return  loginRespond;
        }
        User user1 = userService.selectByUserName(user.getUsername());

        if(user1.getPassword()==null||!SecurityUtil.verify(user.getPassword(),user1.getPassword())){
            loginRespond.setMsg("账号或密码错误");
            loginRespond.setUserId(null);
            return  loginRespond;
        }
        loginRespond.setMsg("登录成功");
        loginRespond.setUserId(user1.getUserId());
        return  loginRespond;
    }

    @RequestMapping("enroll")
    public String  enroll(User user){
        if(user==null|| !StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPassword())||
            !StringUtils.hasLength(user.getPhone())||!StringUtils.hasLength(user.getAddress())) {
            return "请合法输入";
        }
        String enroll = userService.enroll(user);
        if("注册成功".equals(enroll)){
            return "注册成功";
        }else {
            return "注册失败";
        }
    }

}
